<template>
	<view class="wrapper">
		<!-- {{chartData}} -->
		<view class="info-block">
			<view class="info-item">
				<view class="label">设备厂家：</view>
				<view class="value">{{equipmentInfo.url.split('@')[0]}}</view>
			</view>
			<view class="info-item">
				<view class="label">设备型号：</view>
				<view class="value" style="display: flex;justify-content: space-between;width:530upx">
					<span>{{equipmentInfo.url.split('@')[1]}}</span>
					<navigator hover-class="none"
						:url="`/pages/equipment_params/index?equipmentId=${equipmentInfo.id}`">
						<view class="more-params">
							<text>全部参数</text>
							<u-icon name="arrow-right"></u-icon>
						</view>
					</navigator>
				</view>
			</view>
		</view>

		<u-gap height="20" bg-color="#F7F8FA" />

		<view class="tag-block">
			<!-- 设备状态：//0-正常 1-待机 2-低效 3-通信中断 4-其他 -->
			<!-- {{equipmentInfo.status = '3'}} -->
			<view class="tag" style="color: #4290F2;" v-if="equipmentInfo.status === '0'">正常</view>
			<view class="tag" style="color: #6312aa;" v-if="equipmentInfo.status === '1'">待机</view>
			<view class="tag" style="color: #ffaa00;" v-if="equipmentInfo.status === '2'">低效</view>
			<view class="tag" style="color: #ff0000;" v-if="equipmentInfo.status === '3'">通信中断</view>
			<view class="tag" style="color: #34344e;" v-if="equipmentInfo.status === '4'">其他</view>
		</view>

		<view class="chart-block">

			<view class="center-block">
				<u-image src="/static/equipment_detail/power.png" width="40" height="40" />
				<view style="font-size: 34upx;">当前功率</view>
				<view>
					<text style="color:#378FFA;font-size:80upx;">
						{{parseFloat(equipmentInfo.bwyg_value.substring(0,equipmentInfo.bwyg_value.indexOf('@'))).toFixed(2)}}
					</text>
					<text style="font-size: 30upx;">KW</text>
				</view>
				<view>接入功率：{{equipmentInfo.actual_capacity}}KW</view>
			</view>

			<view class="ring-block" v-show="ready">
				<view class="tick" v-for="item in 18" :key="item"
					:style="{'transform':'translate(-50%, -50%) rotate('+(10 * item)+'deg)'}"></view>
			</view>

			<view class="chart-item">
				<qiun-data-charts type="ring" :chartData="chartData" :disableScroll="true" background="none"
					:tooltipShow="false" :tapLegend="false" :ontap="false" :onmouse="false" :opts="opts" />
			</view>

		</view>
		<view class="power-block">
			<view class="item">
				<u-image src="/static/equipment_detail/icon_flash.png" width="40" height="40" />
				<view class="txt">
					<span>今日发电:</span>
					<span style="color:#333;">
						{{parseFloat(equipmentInfo.rfdl_value.substring(0,equipmentInfo.rfdl_value.indexOf('@'))).toFixed(2)}}
					</span>
					<span>KWh</span>
				</view>
			</view>
			<view class="item">
				<u-image src="/static/equipment_detail/icon_chart.png" width="40" height="40" />
				<view class="txt">
					<span>累计发电:</span>
					<span style="color:#333;">
						{{(parseFloat(equipmentInfo.zfdl_value.substring(0,equipmentInfo.zfdl_value.indexOf('@'))) / 1000).toFixed(2)}}
					</span>
					<span>MWh</span>
				</view>
			</view>
		</view>
		<u-gap height="20" bg-color="#F7F8FA" />
		<ucharts-block ref="chartBlock" :device-id="equipmentInfo.id" />
	</view>
</template>

<script>
	import uchartsBlock from '@/components/ucharts-block.vue'
	export default {
		components: {
			uchartsBlock
		},
		data() {
			return {
				ready: false,
				opts: {
					"type": "ring",
					"canvasId": "",
					"canvas2d": false,
					"background": "none",
					"animation": true,
					"timing": "easeOut",
					"duration": 1000,
					"color": [
						"#5cb85c",
						"#cccccc"
					],
					"rotate": false,
					"errorReload": false,
					"fontSize": 12,
					"fontColor": "",
					"enableScroll": false,
					"touchMoveLimit": 60,
					"enableMarkLine": false,
					"dataLabel": false,
					"dataPointShape": false,
					"dataPointShapeType": "solid",
					"tapLegend": false,
					"legend": {
						"show": false,
						"position": "right",
						"float": "center",
						"padding": 0,
						"margin": 0,
						"backgroundColor": "rgba(0,0,0,0)",
						"borderColor": "rgba(0,0,0,0)",
						"borderWidth": 0,
						"fontSize": 13,
						"fontColor": "#666666",
						"lineHeight": 25,
						"hiddenColor": "#CECECE",
						"itemGap": 0
					},
					"title": {
						"name": "",
						"fontSize": 15,
						"color": "",
						"offsetX": 0,
						"offsetY": -20
					},
					"subtitle": {
						"name": "",
						"fontSize": 25,
						"color": "",
						"offsetX": 0,
						"offsetY": 0
					},
					"extra": {
						"ring": {
							"ringWidth": 8,
							"centerColor": "#FFFFFF",
							"activeOpacity": 0,
							"activeRadius": 0,
							"offsetAngle": -90,
							"labelWidth": 0,
							"border": false,
							"borderWidth": 0,
							"borderColor": "#FFFFFF",
							"linearType": "none",
							"customColor": [
								"#FA7D8D",
								"#EB88E2"
							]
						},
						"tooltip": {
							"showBox": true,
							"showArrow": true,
							"showCategory": false,
							"borderWidth": 0,
							"borderRadius": 0,
							"borderColor": "#000000",
							"borderOpacity": 0.7,
							"bgColor": "#000000",
							"bgOpacity": 0.7,
							"gridType": "solid",
							"dashLength": 4,
							"gridColor": "#CCCCCC",
							"fontColor": "#FFFFFF",
							"splitLine": true,
							"horizentalLine": false,
							"xAxisLabel": false,
							"yAxisLabel": false,
							"labelBgColor": "#FFFFFF",
							"labelBgOpacity": 0.7,
							"labelFontColor": "#666666"
						}
					}
				},
				chartData: {
					categories: [],
					series: [{
						data: []
					}]
				},
				stationId: null,
				equipmentInfo: {},
				params: {}
			}
		},
		onLoad(options) {
			if (options.equipmentInfo) {
				this.equipmentInfo = JSON.parse(options.equipmentInfo)
				// console.log(this.equipmentInfo)
				uni.setNavigationBarTitle({
					title: this.equipmentInfo.name
				})
			}
			if (options.stationId) {
				this.stationId = options.stationId
			}
		},
		onReady() {
			let current = parseFloat(this.equipmentInfo.bwyg_value.substring(0,
				this.equipmentInfo.bwyg_value.indexOf('@')))
			let total = parseFloat(this.equipmentInfo.actual_capacity)
			this.chartData.series[0].data = [{
				value: current
			}, {
				value: total - current
			}]
			this.$refs.chartBlock.loadChartInfo()

			setTimeout(() => {
				this.ready = true
			}, 1000)
		},
		onPullDownRefresh() {
			this.$refs.chartBlock.loadChartInfo()
			this.reloadEquipmentInfo()
		},
		methods: {
			/**
			 * 获取设备-(连个详情接口都没有，还从列表取)
			 */
			reloadEquipmentInfo() {
				this.$u.api.getDeviceList({
					stationId: this.stationId
				}).then(res => {
					console.log(111)
					this.equipmentInfo = res.find(item => item.id === this.equipmentInfo.id)
				}).finally(() => {
					uni.stopPullDownRefresh()
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrapper {
		.more-params {
			color: #999;
			display: flex;
			align-items: center;
		}

		.info-block {
			padding: 30upx 40upx;

			.info-item {
				margin-top: 20upx;
				display: flex;

				.label {
					color: #999;
				}

				.value {
					color: #333;
				}
			}
		}

		.tag-block {
			display: flex;
			justify-content: flex-end;
			padding: 30upx 40upx 0 0;

			.tag {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 4upx 10upx;
				height: 46upx;
				background: #f0f0f0;
				border-radius: 21upx;
				font-size: 28upx;
				font-weight: 400;
				line-height: 40upx;
			}
		}

		.chart-block {
			position: relative;
			width: 100%;
			height: 600upx;
			display: flex;
			align-items: center;
			justify-content: center;
			overflow: hidden;

			.ring-block {
				width: 600upx;
				height: 600upx;
				position: relative;
				border-radius: 50%;
				overflow: hidden;
				display: flex;
				align-items: center;
				justify-content: center;

				.tick {
					position: absolute;
					left: 50%;
					top: 50%;
					width: 10upx;
					height: 620upx;
					background: #ccc;
				}
			}

			.center-block {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: space-around;
				padding: 100upx 0;
				position: absolute;
				width: 600upx;
				height: 600upx;
				color: #888;
				border-radius: 50%;
				z-index: 9;
			}

			.chart-item {
				position: absolute;
				width: 560upx;
				height: 560upx;
				border-radius: 50%;
				background: #fff;
			}
		}

		.power-block {
			padding: 80upx 40upx 40upx 40upx;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			justify-content: space-between;

			.item {
				display: flex;
				align-items: center;
				line-height: 60upx;

				.txt {
					color: #999;
					margin-left: 4upx;
				}
			}
		}
	}
</style>
